package com.yxr.job;


import com.yxr.constants.SystemCanstants;
import com.yxr.domain.entity.SgArticle;
import com.yxr.service.SgArticleService;
import com.yxr.utils.RedisCache;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Component

public class UpdateViewCount {

    @Autowired
    private RedisCache redisCache;
    @Autowired
    private SgArticleService articleService;

    @Scheduled(cron = "0/1 5 * * * ?")
    public void uodateViewCount(){
        //获取redis中的浏览量
        Map<String, Integer> cacheMap = redisCache.getCacheMap(SystemCanstants.REDIS_VIEWCOUNT_PREFIX);

        List<SgArticle> collect = cacheMap.entrySet().stream()
                .map(article -> new SgArticle(Long.valueOf(article.getKey()), article.getValue().longValue()))
                .collect(Collectors.toList());
        //更新到数据库
        articleService.updateBatchById(collect);
    }
}
